/**
 * 项目初始化检测并修正
 * .gitignore, 必要组件
 */

import { readFile, writeFile } from 'fs/promises'
import EventRoute from '../EventRoute'
import { resolve } from 'path'

const routes: EventRoute[] = []

routes.push(
  new EventRoute('gitignore', async (_, { path }) => {
    const filename = `.gitignore`
    const gitignore_path = resolve(path as string, filename)
    const separator = `\r\n`
    const content = await readFile(gitignore_path, 'utf-8')
    const rules = content.split(separator)

    const keywords = ['# auto-axure', 'records']
    const contain_records = keywords.every((keyword) => rules.indexOf(keyword) > -1)
    if (!contain_records) {
      rules.push(...keywords)
    }

    const rules_with_records = rules.join(separator)
    await writeFile(gitignore_path, rules_with_records, { encoding: 'utf-8' })
  })
)

export default routes.map((route) => route.setType('check'))
